
// 转成base64
export function getBase64 (file) {
	return new Promise(function (resolve, reject) {
		let reader = new FileReader()
		let imgResult = ""
		reader.readAsDataURL(file)
		reader.onload = function () {
			imgResult = reader.result
		}
		reader.onerror = function (error) {
			reject(error)
		}
		reader.onloadend = function () {
			resolve(imgResult)
		}
	 })
}

/** 将base64转换为文件对象
 *  @param {String} base64 base64字符串
 * */
export function convertBase64ToBlob (base64){
	var base64Arr = base64.split(',');
	var imgtype = '';
	var base64String = '';
	if(base64Arr.length > 1){
			//如果是图片base64，去掉头信息
			base64String = base64Arr[1];
			imgtype = base64Arr[0].substring(base64Arr[0].indexOf(':')+1,base64Arr[0].indexOf(';'));
	}
	// 将base64解码
	var bytes = atob(base64String);
	//var bytes = base64;
	var bytesCode = new ArrayBuffer(bytes.length);
	 // 转换为类型化数组
	var byteArray = new Uint8Array(bytesCode);
	
	// 将base64转换为ascii码
	for (var i = 0; i < bytes.length; i++) {
			byteArray[i] = bytes.charCodeAt(i);
	}
 
	// 生成Blob对象（文件对象）
	return new Blob( [bytesCode] , {type : imgtype});
};